﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Revis.VClinicNet.Util.Extensions
{
    public static class StringExtensions
    {
        public static bool HasOnlyLetters(this string str)
        {
            return str.IsCompatibleWith(Char.IsLetter);
        }

        public static bool HasOnlyDigits(this string str)
        {
            return str.IsCompatibleWith(Char.IsDigit);
        }

        public static bool HasOnlyLettersAndDigits(this string str)
        {
            return str.IsCompatibleWith(Char.IsLetterOrDigit);
        }

        public static bool IsCompatibleWith(this string str, Func<char, bool> condition)
        {
            bool result = true;
            foreach (char c in str) {
                if (condition(c) == false) {
                    result = false;
                    break;
                }
            }
            return result;
        }

        public static bool IsCompatibleWith(this string str, Func<char, bool> condition, char[] additional)
        {
            bool result = true;
            foreach (char c in str) {
                if (condition(c) == false) {
                    if (c.IsOneOf(additional) == false) {
                        result = false;
                        break;
                    }
                }
            }
            return result;
        }
    }
}
